package cn.whitecao.contentcenter.controller;

import cn.whitecao.contentcenter.common.CommonBlockHandler;
import cn.whitecao.contentcenter.entity.Notice;
import cn.whitecao.contentcenter.rocketmq.MySource;
import cn.whitecao.contentcenter.service.INoticeService;
import cn.whitecao.contentcenter.service.ShareService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.RequiredArgsConstructor;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：腼腆的老黄.
 * @date ：Created in 2019-08-13 11:03
 */
@RestController
@RequiredArgsConstructor
public class TestController {

    private final DiscoveryClient discoveryClient;
    private final INoticeService noticeService;
    private final ShareService shareService;
    private final Source source;
    private final MySource mySource;

    @GetMapping("/testMyOutput")
    public String testMyOutput(){
        mySource.output().send(MessageBuilder.withPayload("消息体").build());
        return "success";
    }


    @GetMapping("/testStreamSource")
    public String testStreamSource(){
        source.output().send(MessageBuilder.withPayload("消息体").build());
        return "success";
    }




    /**
     * 测试服务发现
     */
    @GetMapping("/test2")
    public List<ServiceInstance> testDiscoveryClient(){
        List<ServiceInstance> instances = this.discoveryClient.getInstances("user-center");
        return instances;
    }

    @RequestMapping("testSentinel")
    @SentinelResource(value = "test-sentinel-api",
            blockHandler = "block",
            blockHandlerClass = CommonBlockHandler.class,
          fallback = "fallback")
    public String testSentinel(@RequestParam(required = false) String a){
        if(a==null)
                throw new IllegalArgumentException("参数不能为空");
        return "success";
    }


    @RequestMapping("testMyUrlBolckHandler")
  //  @SentinelResource("testMyUrlBolckHandler")
    @Async
    public String testMyUrlBolckHandler(@RequestParam(required = false) String a){
        if(a==null)
            throw new IllegalArgumentException("参数不能为空");
        return "success";
    }
    @RequestMapping("aaaa")
    @Async
    public String aaaa(@RequestParam(required = false) String a){
        System.out.println(Thread.currentThread().getName());
        shareService.asyncTest(null);
        return "success";
    }

    public String fallback(String a){
        System.out.println("限流或者降级了:fallback");
        return "限流或者降级了";
    }


    @RequestMapping("testBatchInsert")
    public void insert(){
        ArrayList<Notice> list = new ArrayList<>();
        for (int i = 0; i < 100000; i++) {
            Notice notice = new Notice();
            notice.setContent(""+i);

            list.add(notice);
        }

        noticeService.saveBatch(list,100000);
    }

}
